#include "qdailytimer.h"

QDailyTimer::QDailyTimer(int h, int m)
{
	// Assign the time values to proper members
    this->assignTimeValues(h, m);

    // By default the timer will timeout on weekends
    _timeout_on_weekends = true;

    // Set the internal timer to check every minute
    _internaltimer = new QTimer(this);
    _internaltimer->setInterval(1000*60);
    _internaltimer->setSingleShot(false);
    connect(_internaltimer, SIGNAL(timeout()), this, SLOT(internalTimer_timedout()));
}

void QDailyTimer::start()
{
    this->initTimer();
    _internaltimer->start();
}

void QDailyTimer::stop()
{
    _internaltimer->stop();
}

void QDailyTimer::reschedule(int h, int m)
{
	// Assign the time values to proper members
	this->assignTimeValues(h, m);

	// Initialize again according to the new schedule
	this->initTimer();
}

void QDailyTimer::assignTimeValues(int h, int m)
{
	// Check and assign hours' value
	if ((h >=0) && (h <=23))
	{
		_hours = h;
	} else {
		throw "Invalid hours value passed to QDailyTimer.";
	}

	// Check and assign minutes' value
	if ((m >=0) && (m <=59))
	{
		_minutes = m;
	} else {
		throw "Invalid minutes value passed to QDailyTimer.";
	}

	// Seconds' value will always be zero
	_seconds = 0;
}

void QDailyTimer::initTimer()
{
    QDateTime* now = new QDateTime(QDateTime::currentDateTime());
    QDateTime* dt = new QDateTime(QDateTime::currentDateTime());
    dt->setTime(QTime(_hours, _minutes, _seconds));

    // If the time has passed today already then schedule for tomorrow
    if (*dt <= *now) {
        *dt = dt->addDays(1);
    }

    // Set the next schedule
    uint ts = dt->toTime_t() - now->toTime_t();
    _nextschedule = new QDateTime(*now);
    _nextschedule->setTime_t(now->toTime_t() + ts);
}

void QDailyTimer::internalTimer_timedout()
{
    if (isNowTheTime())
    {
        initTimer();

        // Check the weekend timeout flag before emitting
        if (_timeout_on_weekends == true)
        {
            emit timeout(); // Emit everyday
        } else {
            // Emit only if not weekend
            QDateTime* now = new QDateTime(QDateTime::currentDateTime());
            if(now->date().dayOfWeek() <= (int)Qt::Friday)
                emit timeout();
        }
    }
    qDebug() << "QDailyTimer::internatTimer_Elapsed() was called.";
}

bool QDailyTimer::isNowTheTime()
{
    bool year=false, month=false, day=false, hour=false, minute=false;
    QDateTime* now = new QDateTime(QDateTime::currentDateTime());

    year = (now->date().year() == _nextschedule->date().year()) ? true:false;
    month = (now->date().month() == _nextschedule->date().month()) ? true:false;
    day = (now->date().day() == _nextschedule->date().day()) ? true:false;
    hour = (now->time().hour() == _nextschedule->time().hour()) ? true:false;
    minute = (now->time().minute() == _nextschedule->time().minute()) ? true:false;

    if (year && month && day && hour && minute)
        return true;
    else
        return false;
}

QDateTime* QDailyTimer::nextSchedule()
{
    return new QDateTime(*_nextschedule);
}

void QDailyTimer::setInterval(int msec)
{
    QTimer::setInterval(msec);
}

bool QDailyTimer::getTimeoutOnWeekends()
{
    return _timeout_on_weekends;
}

void QDailyTimer::setTimeoutOnWeekends(bool flag)
{
    _timeout_on_weekends = flag;
}
